package com.nave.utils;

import java.util.ArrayList;
import java.util.HashMap;

import nave.control.Options;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

import com.nave.R;

public class SoundController
{
	/*
	 * Attributes
	 */
	private static SoundController singleton = null;
	
	public static final int TURBINE			=  0;
	public static final int LOAD_TURBINES	=  1;
	public static final int EXPLOSION		=  2;
	public static final int HIT_BLOCK		=  3;
	public static final int BLOCK_EXPLOSION	=  4;
	public static final int GAME_OVER		=  5;
	public static final int SHOT			=  6;
	public static final int VICTORY			=  7;
	public static final int NEW_RECORD		=  8;
	public static final int CLICK			=  9;
	
	private ArrayList<Integer> backgroundSound;
	private SoundPool soundPool;
	private HashMap<Integer, Integer> soundMap;
	
	/*
	 * Constructor
	 */
	private SoundController()
	{
		backgroundSound = new ArrayList<Integer>();
		
		final int maxSimultaneousSounds = 10;
        soundPool = new SoundPool(maxSimultaneousSounds, AudioManager.STREAM_MUSIC, 100);
        
        final int maxNumberOfSounds = 12;
        soundMap = new HashMap<Integer,Integer>(maxNumberOfSounds);
	}
	
	/*
	 * Public methods
	 */
	public static SoundController getReference()
	{
		if ( singleton == null ) singleton = new SoundController();
		
		return singleton;
	}

	public void playSound(int sound, Context c, int loop)
	{
		if ( Options.sound() )
		{
			/* The next 4 lines calculate the current volume in a scale of 0.0 to 1.0 */
		    AudioManager mgr = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);
		    float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
		    float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		    float volume = streamVolumeCurrent / streamVolumeMax;
	
		    /* Play the sound with the correct volume */
		    int streamID = soundPool.play( soundMap.get(sound),
		    		volume, volume, 1, loop, 1f);
	
		    if ( loop == -1 )
		    	backgroundSound.add(streamID);
		}
	}
	public void playSound(int sound, Context c)
	{
	    playSound(sound, c, 0);
	}
	
	public void resumeBackgroundSound()
	{
		if ( Options.sound() )
		{
			for ( Integer i : backgroundSound )
			{
				soundPool.resume( i );
			}
		}
	}
	public void pauseBackgroundSound()
	{
		for ( Integer i : backgroundSound )
		{
			soundPool.pause( i );
		}
	}

	public void initSounds(Context context)
	{
		initSound(context, CLICK, R.raw.click);
		initSound(context, TURBINE, R.raw.turbines);
        initSound(context, SHOT, R.raw.shot);
		initSound(context, EXPLOSION, R.raw.explosion);
		initSound(context, GAME_OVER, R.raw.gameover);
		initSound(context, VICTORY, R.raw.victory);
		initSound(context, NEW_RECORD, R.raw.click); // TODO
		initSound(context, LOAD_TURBINES, R.raw.loadingturbines);
		initSound(context, HIT_BLOCK, R.raw.hitblock);
		initSound(context, BLOCK_EXPLOSION, R.raw.explodeblock);
	}

	/*
	 * Private methods
	 */
	private void initSound(Context context, int sound, int resource)
	{
		soundMap.put( sound,
				soundPool.load(context, resource, 1) );
	}

}
